<template>
  <div class="app-container">
    <div class="header-box">
      <div class="header-left">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="代理商名称" prop="name">
                <el-input
                  v-model="queryParams.name"
                  placeholder="请输入代理商名称"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系人姓名" prop="userName">
                <el-input
                  v-model="queryParams.userName"
                  placeholder="请输入联系人姓名"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系人手机号" prop="phone">
                <el-input
                  v-model="queryParams.phone"
                  placeholder="请输入联系人手机号"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="代理商状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择代理商状态" clearable>
              <el-option
                v-for="dict in dict.type.sys_agent"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="queryParams.email"
              placeholder="请输入邮箱"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-form>
      </div>
      <div class="button-right buttons ">
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <el-divider></el-divider>
    <div class="button-right">
      <el-button
        type="primary"
        size="small"
        icon="el-icon-download"
        @click="handleExport"
        v-hasPermi="['agent:agentManage:export']"
      >导出</el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-plus"
        v-hasPermi="['agent:agentManage:add']"
        @click="handleAdd()"
      >新增</el-button>
    </div>
    <el-table v-loading="loading" :data="agentManageList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column label="代理商名称" align="center" prop="name" width="100" />
      <el-table-column label="姓名" align="center" prop="userName" width="100" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" width="200" />
      <el-table-column label="代理商地址" align="center" prop="address" />
      <el-table-column label="代理商余额" align="center" prop="balance" width="100" />
      <el-table-column label="关联商家数" align="center" prop="storeNum" width="100" />
      <el-table-column label="后台功能权限" align="center" prop="authorize">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            style="color: #017EFF;"
            @click="goPermissionSettings(scope.row)"
          >设置</el-button>
        </template>
      </el-table-column>
<!--      <el-table-column label="标识" align="center" prop="mark" />-->
<!--      <el-table-column label="密码" align="center" prop="password" />-->
<!--      <el-table-column label="前台域名" align="center" prop="frontDomain" />-->
<!--      <el-table-column label="后台域名" align="center" prop="afterDomain" />-->
<!--      <el-table-column label="法人身份证图片地址" align="center" prop="cardUrl" />-->
<!--      <el-table-column label="代理商logo图片地址" align="center" prop="logoUrl" />-->
<!--      <el-table-column label="营业执照图片地址" align="center" prop="licenseUrl" />-->
<!--      <el-table-column label="承诺函图片地址" align="center" prop="commitmentUrl" />-->
      <el-table-column label="代理商状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeStatus(scope.row)">
          </el-switch>
          <!-- <dict-tag :options="dict.type.sys_agent" :value="scope.row.status"/> -->
        </template>
      </el-table-column>
<!--      <el-table-column label="有效期开始时间" align="center" prop="startTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="有效期结束时间" align="center" prop="endTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            style="color: #017EFF;"
            icon="el-icon-edit"
            @click="resetPassword(scope.row)"
            v-hasPermi="['agent:agentManage:edit']"
          >重置密码</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #017EFF;"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['agent:agentManage:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #017EFF;"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['agent:agentManage:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改管理对话框 -->
    <el-dialog title="重置密码" :visible.sync="open" width="400px" append-to-body>
      <el-form :model="form">
        <el-form-item label="密码">
<!--          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input> -->
          <el-input v-model="form.password" type="password" auto-complete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAgentManage, getAgentManage, delAgentManage, addAgentManage, updateAgentManage, resetStatus } from "@/api/agent/agentManage";

export default {
  name: "AgentManage",
  dicts: ['sys_agent'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管理表格数据
      agentManageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        userName: null,
        email: null,
        phone: null,
        city: null,
        status: null,
        startTime: null,
        endTime: null,
      },
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询管理列表 */
    getList() {
      this.loading = true;
      listAgentManage(this.queryParams).then(response => {
        this.agentManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        userName: null,
        email: null,
        phone: null,
        city: null,
        address: null,
        balance: null,
        storeNum: null,
        authorize: null,
        mark: null,
        password: null,
        frontDomain: null,
        afterDomain: null,
        cardUrl: null,
        logoUrl: null,
        licenseUrl: null,
        commitmentUrl: null,
        status: null,
        startTime: null,
        endTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: "/agentManage/addAgent", query: { type: 'add'} });
      // this.reset();
      // this.open = true;
      // this.title = "添加管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/agentManage/addAgent', query: { type: 'edit', id: row.id || this.ids } });
      // this.reset();
      // const id = row.id || this.ids
      // getAgentManage(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改管理";
      // });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateAgentManage(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addAgentManage(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除管理编号为"' + ids + '"的数据项？').then(function() {
        return delAgentManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('agent/agentManage/export', {
        ...this.queryParams
      }, `agentManage_${new Date().getTime()}.xlsx`)
    },
    // 跳转后台功能权限设置
    goPermissionSettings(row) {
      this.$router.push({ path: "/agentManage/permissionSettings", query: { id: row.id } });
    },
    // 重置密码
    resetPassword(row) {
      // const id = row.id || this.ids
      getAgentManage(row.id).then(response => {
        this.form = response.data;
        this.form.password = '',
        this.open = true;
      });
    },
    submitForm() {
      updateAgentManage(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    // 状态开关
    changeStatus(row) {
      let params = {};
      params.agentId = row.id;
      params.status = row.status;
      resetStatus(params).then(response => {
        this.getList();
      });
    },
  }
};
</script>

<style lang="scss" scoped>
  .header-box {
    height: 80px;
  }
  .buttons {
    float: right;
    transform: translateY(45px);
  }
</style>
